package cn.kami.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kami.dao.IBatteryVersionDAO;
import cn.kami.vo.BatteryVersion;

public class BatteryVersionDAOImpl implements IBatteryVersionDAO {
	
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	public BatteryVersionDAOImpl(Connection conn){
		this.conn = conn;
	}
	
	@Override
	public BatteryVersion findByVersion(String version) throws Exception {
		// TODO Auto-generated method stub
		String sql = "SELECT bat_version,bat_type,rate_voltage,work_voltage,rate_volume,weight FROM BatteryVersion WHERE bat_version=?";
		this.pstmt = this.conn.prepareStatement(sql);
		pstmt.setString(1, version);
		ResultSet rs = this.pstmt.executeQuery();
		BatteryVersion batVersion = null;
		while(rs.next()){
			batVersion = new BatteryVersion();
			batVersion.setBatVersion(rs.getString(1));
			batVersion.setBatType(rs.getString(2));
			batVersion.setRateVoltage(rs.getString(3));
			batVersion.setWorkVoltage(rs.getString(4));
			batVersion.setRateVolume(rs.getInt(5));
			batVersion.setWeight(rs.getInt(6));
		}
		this.pstmt.close();
		return batVersion;
	}


	@Override
	public List<BatteryVersion> findAllVersionName() throws Exception {
		// TODO Auto-generated method stub
		List<BatteryVersion> all = new ArrayList<BatteryVersion>();
		String sql = "SELECT bat_version FROM batteryversion";
		this.pstmt = this.conn.prepareStatement(sql);
		ResultSet rs = this.pstmt.executeQuery();
		BatteryVersion batVersion = null;
		while(rs.next()){
			batVersion = new BatteryVersion();
			batVersion.setBatVersion(rs.getString(1));
			all.add(batVersion);
		}
		this.pstmt.close();
		return all;
	}

}
